package _ihm;
import src.Individu;
import src.Map;

import javax.imageio.ImageIO;
import javax.swing.*;

import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Enumeration;
import java.util.Observer;




public class ihm_map extends JPanel implements ActionListener {
	
	private GridBagLayout gb ;
	private GridBagConstraints gc;
	private String PathImages;
	public int SIZE;
	private ihm_cell[][] cell;
	public ihm_frame frame;
	private String[] tableauObjets = new String[5];
	private Map map;

	public String[] getTableauObjets() {
		return tableauObjets;
	}

	public void setTableauObjets(String[] tableauObjets) {
		this.tableauObjets = tableauObjets;
	}

	public ihm_map() {
		
	}
	
	public ihm_map(Map map) {
		
		this.map = map;
		setToolTipText("Carte du Jeu");
		SIZE = this.map.getSize();
		cell= new ihm_cell[SIZE][SIZE];
		gb = new GridBagLayout();
		setLayout(gb);
		
		gc = new GridBagConstraints();
		gc.fill = GridBagConstraints.BOTH;
		
		
		
		File f;
		URL location = ihm_menu.class.getProtectionDomain().getCodeSource().getLocation();
		
    	try { f = new File(location.toURI());
    		} catch(URISyntaxException e1) { f = new File(location.getPath()); JOptionPane.showMessageDialog(null, "Path version 2"); }
    	
    	PathImages = f.getAbsolutePath()+"\\_ihm\\images\\";
		
		for (int i = 0; i<SIZE; i++) {
				for (int j = 0; j<SIZE; j++) {
					{						
						String ElemSprite;
	                    if (map.getCell(i, j).getFond()!= null)
	                    	ElemSprite = map.getCell(i, j).getFond().getSprite();
	                    else
	                    	ElemSprite = "noir.png";
	                    
						if (map.getCell(i, j).getIndividu() != null)
							ElemSprite =map.getCell(i, j).getIndividu().getSprite();
						
						if (!map.getCell(i, j).getObjet().isEmpty()){
							Enumeration<String> keys = map.getCell(i, j).getObjet().keys();
							
						if(keys.hasMoreElements()){
								String key = keys.nextElement();
								ElemSprite =map.getCell(i, j).getObjet().get(key).getSprite();
							}	
						}
	                	
	                	
	                	try {
	                		Image Img = ImageIO.read(new File(PathImages+ElemSprite));
	                		
							cell[i][j] = new ihm_cell(i,j,Img,this);
							
	                		gc.gridx = i;
				            gc.gridy = j;
				            gc.gridheight = 1;
				            gc.gridwidth = 1;
		                   
				            gb.setConstraints(cell[i][j].getLbl(),gc);
		                    add(cell[i][j].getLbl(),gc);

	                		} catch (IOException e1) {
	                			e1.printStackTrace();
	                		JOptionPane.showMessageDialog(null, "Echec de l'ajout des images" +PathImages+ElemSprite);}
	                	
	                    }
					}
		}
		
		
	}
	public void addObjetMap(ihm_cell ObjCel){
		if (ObjCel.isEtat())
		{
			int qte = Integer.valueOf(getTableauObjets()[2]);
			//JOptionPane.showMessageDialog(null, "getTableauObjets()[1] : " + getTableauObjets()[1]+" getMap().getCell(X,Y) : "+ getMap().getCell(ObjCel.getX(),ObjCel.getY())+" qte : "+qte);
			frame.getController().addObjectToCell(getTableauObjets()[1], getMap().getCell(ObjCel.getX(),ObjCel.getY()), qte );
		}
	}

	public Map getMap() {
		return map;
	}

	public void setMap(Map map) {
		this.map = map;
	}

	public void setCell(ihm_cell[][] cell) {
		this.cell = cell;
	}

	public ihm_frame getFrame() {
		return frame;
	}

	public void setFrame(ihm_frame frame) {
		this.frame = frame;
	}

	public ihm_cell[][] getCell() {
		return cell;
	}
	public void ActionValide(boolean etat) {
		for (int i = 0; i<SIZE; i++) {
			for (int j = 0; j<SIZE; j++) 
				cell[i][j].setEtat(etat);
		}
	}
	public ihm_cell getCell(int i , int j) {
		return cell[i][j];
	}

	public void setCell(ihm_cell cell, int i , int j) {
		this.cell[i][j] = cell;
	}
		@Override
		public void actionPerformed(ActionEvent arg0) {
			JOptionPane.showMessageDialog(null, ((JButton)arg0.getSource()).getX());
			JOptionPane.showMessageDialog(null, ((JButton)arg0.getSource()).getY());
		}
	//@Override
//		public ihm_map update(Map NewMap) {
//			return (new ihm_map(NewMap));
//			 			
//		}
		
		public void updateMap(Map NewMap) {
			 	map = NewMap;		
		}
		
		public void repaint(){
			for (int i = 0; i<SIZE; i++) {
				for (int j = 0; j<SIZE; j++) {
					cell[i][j].revalidate();
					cell[i][j].repaint();
				}
			}
		}
		
		public void updateAffichageMap() {
			//droit d acces
			File f;
			URL location = ihm_menu.class.getProtectionDomain().getCodeSource().getLocation();
			
	    	try { f = new File(location.toURI());
	    		} catch(URISyntaxException e1) { f = new File(location.getPath()); JOptionPane.showMessageDialog(null, "Path version 2"); }
	    	
	    	PathImages = f.getAbsolutePath()+"\\_ihm\\images\\";
			
	    	//boucle parcour grille jpanel
			for (int i = 0; i<SIZE; i++) {
				for (int j = 0; j<SIZE; j++) {
					String ElemSprite;
                    if (map.getCell(i, j).getFond()!= null)
                    	ElemSprite = map.getCell(i, j).getFond().getSprite();
                    else
                    	ElemSprite = "noir.png";
                    
					if (map.getCell(i, j).getIndividu() != null)
						ElemSprite =map.getCell(i, j).getIndividu().getSprite();
					
					if (!map.getCell(i, j).getObjet().isEmpty()){
						
						Enumeration<String> keys = map.getCell(i, j).getObjet().keys();
						if(keys.hasMoreElements()){
							String key = keys.nextElement();
							ElemSprite =map.getCell(i, j).getObjet().get(key).getSprite();
						}	
					}
                	
                	try {
                		Image img = ImageIO.read(new File(PathImages+ElemSprite));
                		cell[i][j].setLblImg(img);
                		cell[i][j].revalidate();
                		//cell[i][j].getLbl().repaint();
                	}catch(Exception e){
                		e.printStackTrace();
                	}
				}
			}
		}
		
//		public void drawGrille(Graphics g){
//			int xCentre;
//			int yCentre;
//		}

	}